package com.jackchong.base;

import android.app.Activity;
import android.os.Bundle;

import androidx.annotation.IdRes;
import androidx.annotation.LayoutRes;
import androidx.annotation.Nullable;

import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import com.gyf.barlibrary.ImmersionBar;
import com.jackchong.utils.AndroidBug5497Workaround;
import com.jackchong.utils.JAuto;;
import com.jackchong.utils.DeviceInfo;
import com.jackchong.widget.EventFrameLayout;
import com.jackchong.widget.JFrameLayout;
import com.jackchong.widget.MainEventLayout;
import com.utils.R;

import butterknife.ButterKnife;
import butterknife.Unbinder;


/**
 * 作者: jack(黄冲)
 * 邮箱: 907755845@qq.com
 * create on 2017/6/17
 */

public abstract class BaseLayoutActivity extends BaseToolsActivity {

    private EventFrameLayout mLayoutChild;
    private EventFrameLayout mErrorView;
    private JFrameLayout mLlHeader;
    private JFrameLayout mMianLayout;
    private MainEventLayout mMainEventView;
    private Unbinder mUnbinder;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initToolbarColor();

        JAuto.setSize(this, false, DeviceInfo.UI_WIDTH, DeviceInfo.UI_HEIGHT);

        super.setContentView(R.layout.act_base_layout);

        initView();

        JAuto.auto(this);

        onCreateView(savedInstanceState);

        initSoftKeyboardSeting();

    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    private void initSoftKeyboardSeting() {
        if (autoSoftKeyboard()){
            AndroidBug5497Workaround.assistActivity(mActivity);
        }
    }


    /**
     * time    : 2019/3/27 11:38
     * desc    : 是否适配软键盘, 默认true
     * versions: 1.0
     */
    protected boolean autoSoftKeyboard(){
        return true;
    }

    protected void initToolbarColor() {
        ImmersionBar.with(this).statusBarDarkFont(true).init();
    }


    private void initView() {
        mLlHeader = (JFrameLayout) findViewById(R.id.layout_header);

        int headerLayout = getHeaderLayout();

        if (headerLayout != 0) {
            loadHeader(headerLayout);
        }else {
            mLlHeader.setVisibility(View.GONE);
        }

        mMianLayout = findViewById(R.id.layout_mian);
        mMainEventView = findViewById(R.id.mainEventLinearLayout);
        mLayoutChild = findViewById(R.id.layout_child);
        mErrorView = findViewById(R.id.layout_error);
        mMainEventView.setOnTouchEvent(this::mainTouchEvent);
        mMainEventView.setOnDownListener(this::mainDownEvent);


        int count = 0;
        for (Activity activity : AppManager.getInstance().getAllActivity()) {
            if (activity != null && !activity.isFinishing() && !activity.isDestroyed()){
                count++;
            }
        }
        setSwipeBackEnable(count > 1);
    }


    private void loadHeader(int headerLayout) {
        initHeader(addHeadleView(headerLayout));
    }


    /**
     * time    : 2019/2/20 9:48
     * desc    : 隐藏头部
     * versions: 1.0
     */
    protected void hideHeader(){
        mLlHeader.setVisibility(View.GONE);
        ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) mMianLayout.getLayoutParams();
        layoutParams.topMargin = 0;
        mMianLayout.setLayoutParams(layoutParams);
    }

    /**
     * time    : 2019/2/20 9:48
     * desc    : 显示头部
     * versions: 1.0
     */
    protected void showHeader(){
        mLlHeader.setVisibility(View.VISIBLE);
        ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) mMianLayout.getLayoutParams();
        layoutParams.topMargin = getHeaderHeight(mLlHeader);
        mMianLayout.setLayoutParams(layoutParams);
    }



    /**
    * time    : 2019/2/20 9:52
    * desc    : 添加头部
    * @param layout : 头部布局
    * versions: 1.0
    */
    protected View addHeadleView(@LayoutRes int layout) {
        mLlHeader.removeAllViews();
        return View.inflate(mActivity, layout, mLlHeader);
    }

    /**
     * time    : 2019/2/20 9:52
     * desc    : 添加头部
     * @param view : 头部View
     * versions: 1.0
     */
    protected View addHeadleView(View view) {
        if (view == null) return null;
        mLlHeader.removeAllViews();
        mLlHeader.addView(view);
        return view;
    }

    @Override
    @Deprecated
    /**
     * 请使用 addContentView()
     */
    public final void setContentView(int layoutResID){
        throw new IllegalStateException("请使用 addContentView");
    }

    /**
     * time    : 2019/2/20 9:58
     * desc    : 将子类布局添加到基类布局中
     * @param layout 布局
     * versions: 1.0
     */
    protected void addContentView(@LayoutRes int layout) {
        addContentView(layout, true);
    }

    /**
     * time    : 2019/2/20 9:58
     * desc    : 将子类布局添加到基类布局中
     * @param layout 布局
     * @param autoLayout 是否自动适配屏幕
     * versions: 1.0
     */
    protected void addContentView(@LayoutRes int layout, boolean autoLayout) {
        addContentView(layout, autoLayout, false);
    }



    /**
     * time    : 2019/2/20 9:58
     * desc    : 将子类布局添加到基类布局中
     * @param layout 布局
     * @param autoLayout 是否自动适配屏幕
     * @param isCover  是否已屏幕顶部为起点实现
     * versions: 1.0
     */
    protected void addContentView(@LayoutRes int layout, boolean autoLayout, boolean isCover) {
        View child = LayoutInflater.from(mActivity).inflate(layout, mLayoutChild, true);
        mUnbinder = ButterKnife.bind(this);
        if (mLlHeader.getVisibility() == View.VISIBLE){
            int measuredHeight = getHeaderHeight(mLlHeader);
            ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) mMianLayout.getLayoutParams();
            params.topMargin = isCover ? 0 : measuredHeight;
            mMianLayout.setLayoutParams(params);
        }
        if (autoLayout) JAuto.auto(child, this::onAutoSize);
    }

    protected void setErrorView(@LayoutRes int layout){
        setErrorView(inflateView(layout), true);
    }

    protected void setErrorView(@LayoutRes int layout, boolean autoLayout){
        setErrorView(inflateView(layout), autoLayout);
    }

    protected void setErrorView(View child, boolean autoLayout){
        if (autoLayout) JAuto.auto(child, this::onAutoSize);
        mErrorView.addView(child);
        mLayoutChild.setVisibility(View.INVISIBLE);
    }

    protected void setChildInterceptEvent(boolean intercept){
        mLayoutChild.setInterceptEvent(intercept);
    }

    public void setLayoutHide(){
        mLlHeader.setVisibility(View.GONE);
    }

    public void setLayoutShow(){
        mLlHeader.setVisibility(View.VISIBLE);
    }

    public void hideError(){
        mErrorView.setVisibility(View.GONE);
        mLayoutChild.setVisibility(View.VISIBLE);
    }

    public void showError(){
        mErrorView.setVisibility(View.VISIBLE);
        mLayoutChild.setVisibility(View.INVISIBLE);
    }


    protected boolean onAutoSize(View view, @IdRes int id){
        return true;
    }


    public int getHeaderHeight(View view) {
        View childAt = ((ViewGroup) view).getChildAt(0);
        if (childAt.getLayoutParams().height >= 0 || childAt.getMeasuredHeight() != 0){
            return ((ViewGroup) view).getChildAt(0).getLayoutParams().height;
        }

        if (childAt.getLayoutParams().height < 0 && childAt.getMeasuredHeight() == 0){
            int measureSpec = View.MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, View.MeasureSpec.AT_MOST);
            childAt.measure(measureSpec, measureSpec);
        }
        return childAt.getMeasuredHeight();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mUnbinder != null) mUnbinder.unbind();
        ImmersionBar.with(this).destroy();
    }

    protected abstract void onCreateView(Bundle savedInstanceState);

    protected abstract int getHeaderLayout();

    protected abstract void initHeader(View headerView);

    public void mainTouchEvent(MotionEvent event){

    }

    public void mainDownEvent(MotionEvent event){
        if (mLayoutChild.getChildCount() > 0 && JAuto.isAuto(mLayoutChild.getChildAt(0))) {
            hideInputKeyVisibility(100);
        }
    }
}
